﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
//using System.Linq;
using System.Security.Cryptography;
using System.Text;
//using System.Threading.Tasks;

namespace crpt
{
    class Program
    {
        private static char[] codeTab = new char[256];

        private static void InitCodeTab()
        {
            for (int i = 97; i <= 122; i++)
            {
                char upper = (char)(i - 97 + 65);
                codeTab[i] = upper;
                codeTab[upper] = upper;
            }

            for (int i = 48; i <= 57; i++)
            {
                codeTab[i] = (char)i;
            }
            codeTab[32] = ' ';
            codeTab[44] = ',';
            codeTab[46] = '.';
        }

        private static String toStrTab(String s)
        {
            char lastCh = '\0';
            StringBuilder sb = new StringBuilder(s.Length);
            int length = s.Length;
            for (int i = 0; i < length; i++)
            {
                char ch = s[i];
                if (ch < 'Ā')
                {
                    ch = codeTab[ch];
                    if ((ch == 0) || ((lastCh == ' ') && (ch == ' ')))
                        continue;
                    lastCh = ch;
                    sb.Append(ch);
                }
            }

            return sb.ToString().Trim();
        }

        static void Main(string[] args)
        {
            string str = args[0];
            InitCodeTab();
            //string str = "200001|pdsb|20121017105425|2012101700000078|pdsb0001|pdsb_pds|01|3042060759||20121017|";
            str = toStrTab(str);

            byte[] hash;
            using (MD5 md5 = MD5.Create())
            {
                hash = md5.ComputeHash(Encoding.Default.GetBytes(str));
            }
            byte[] dest = new byte[8];
            for (int i = 0; i < 8; i++)
            {
                dest[i] = (byte)(hash[i] ^ hash[8 + i]);
            }

            byte[] key = new byte[8];

            //7B 2F EF C3 04 C2 BC 9D
            key[0] = 0x7b;
            key[1] = 0x2f;
            key[2] = 0xef;
            key[3] = 0xc3;
            key[4] = 0x04;
            key[5] = 0xc2;
            key[6] = 0xbc;
            key[7] = 0x9d;

            var des = DES.Create();
            des.Mode = CipherMode.ECB;

            des.Padding = PaddingMode.None;
            des.Key = key;
            var encryptor1 = des.CreateEncryptor();

            byte[] outBlock = encryptor1.TransformFinalBlock(dest, 0, dest.Length);

            Console.Write(BitConverter.ToString(outBlock).Replace("-", string.Empty));
        }
    }
}
